﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace CreditCalc
{
    class Program
    {
        static void DiffPaymant(double CreditAmount, double CreditDuration, double InterestRate)
        {
            double VD, OOD = CreditAmount, SNP, pay;
            VD = CreditAmount / CreditDuration;

            DateTime dt = DateTime.Now;
            for (int i = 0; i < CreditDuration; i++)
            {
                SNP = (OOD * (InterestRate/100)) / 12;
                pay = VD + SNP;
                OOD -= pay;
                if (dt.DayOfWeek.ToString() == "Sunday")
                {
                    dt = dt.AddDays(1);
                }
                else if (dt.DayOfWeek.ToString() == "Saturday")
                {
                    dt = dt.AddDays(2);
                }
                Console.WriteLine("Amount of {0:0.##} should be paid on {1}", pay, dt.ToShortDateString());
                dt = dt.AddMonths(1);
            }
        }

        static void AnnuitetPaimant(double CreditAmount, double CreditDuration, double InterestRate)
        {
            double interestkoeff = InterestRate / 12 / 100;
            double annuitetKoeff = (interestkoeff * Math.Pow((1 + interestkoeff), CreditDuration)) / (Math.Pow((1 + interestkoeff), CreditDuration) - 1);
            double A = annuitetKoeff * CreditAmount;
            DateTime dt = DateTime.Now;
            for (int i = 0; i < CreditDuration; i++)
            {
                if (dt.DayOfWeek.ToString() == "Sunday")          
                {
                    dt = dt.AddDays(1);
                }
                else if (dt.DayOfWeek.ToString() == "Saturday")
                {
                    dt = dt.AddDays(2);
                }
                Console.WriteLine("Amount of {0:0.##} should be paid on {1}", A, dt.ToShortDateString());
                dt = dt.AddMonths(1);
            }
        }

        static double GetCreditAmount()
        {
            Console.WriteLine("Please input amount of money you would like to borrow");
            try
            {
                return Convert.ToDouble(Console.ReadLine());
            }
            catch (FormatException)
            {
                Console.WriteLine("Wrong input - please re-type the value");
                return GetCreditAmount();
            }
         }

        static double GetCreditDuration()
        {
            Console.WriteLine("Please input for what time period you would like to get credit");
            try
            {
                return Convert.ToDouble(Console.ReadLine());
            }
            catch (FormatException)
            {
                Console.WriteLine("Wrong input - please re-type the value");
                return GetCreditDuration();
            }
        }

        static double GetInterestRate()
        {
            Console.WriteLine("Please input the interest rate in percents ex: 12 what will be equal to 12%");
            try
            {
                return Convert.ToDouble(Console.ReadLine());
            }
            catch (FormatException)
            {
                Console.WriteLine("Wrong input - please re-type the value");
                return GetInterestRate();
            }
        }

        static void Main(string[] args)
        {
            double CreditAmount = (GetCreditAmount());
            double CreditDuration = (GetCreditDuration());
            double InterestRate = (GetInterestRate());
            Console.Clear();
            AnnuitetPaimant(CreditAmount, CreditDuration, InterestRate);
            DiffPaymant(CreditAmount, CreditDuration, InterestRate);
            Console.ReadKey();
        }
    }
}
